<?php
!function_exists('adminmsg') && exit('Forbidden');
include(H_P.'include/gift/require/func_gift.php');
$P_tmp = H_P."include/gift/template/";
$basename_p = $basename."&action=app&appid=".$app['appid'];
InitGP(array('action_p'));
if(!file_exists(D_P.'data/bbscache/hack_gift_config.php'))
{
	updatecache_cd();
}
include(D_P.'data/bbscache/hack_gift_config.php');

if(empty($action_p))
{
	if(empty($_POST['save']))
	{
		require_once(R_P.'require/credit.php');
		$credit_arr = $credit->cType;
		foreach($credit_arr as $key => $value)
		{
			$options .= "<option value=$key ".($key == $gift_credit ? 'selected' : '').">".$value."</option>";
			$addoptions .= "<option value=$key ".($key == $gift_addcredit ? 'selected' : '').">".$value."</option>";
		}
		
		ifcheck($gift_giftopen,'giftopen');
		ifcheck($gift_acceptopen,'acceptopen');
		ifcheck($gift_randopen,'randopen');
		
		include(PW_SNS_GIFT.'template/admin.htm');
	}
	else
	{
		InitGP(array('config'));
		
		updatecache_cd($config);
		$basename = $basename_p;
		adminmsg('operate_success');
	}
}elseif($action_p=='list')
{
	if(empty($_POST['save']))
	{
		InitGP(array('page'));
		$db_perpage = 10;
		require_once(R_P.'require/forum.php');
		$page=GetGP('page');
		$page = Intval($page);
		$page < 1 && $page = 1;
		$rt = $db->get_one("SELECT count(*) as num FROM pw_sns_gift_list");
		$limit = "LIMIT ".($page-1)*$db_perpage.",".$db_perpage;
		$pages = numofpage($rt['num'],$page,ceil($rt['num']/$db_perpage),"$basename_p&action_p=list&");
		
		$arr_list = array();
		$query = $db->query("SELECT * FROM pw_sns_gift_list order by orderid desc $limit");
		while($rt = $db->fetch_array($query))
		{
			$arr_list[] = $rt; 	
		}
		include(PW_SNS_GIFT.'template/admin.htm');
	}
	else
	{
		InitGP(array('selectid'));
		if($selectid =checkselid($selectid))
		{
			$query = $db->query("SELECT icon FROM  pw_sns_gift_list WHERE pid IN($selectid)");
			while ($rt = $db->fetch_array($query)) {
				if ($rt['icon']) {
					P_unlink($attachdir.'/hack_gift_goods/'.$rt['icon']);
				}
			}
			$db->update("Delete From pw_sns_gift_list Where pid in($selectid)");
		}
		$basename = $basename_p.'&action_p=list';
		adminmsg('operate_success');	
	}
}
elseif($action_p=='add')
{
	if(empty($_POST['save']))
	{
		require_once(R_P.'require/credit.php');
		$credit_arr = $credit->cType;
		$src = '';
		$rt = array();
		$ifopen_Y = 'checked';
		include(PW_SNS_GIFT.'template/admin.htm');
	}
	else
	{
		InitGP(array('subject','srcimg','price','fprice','ifopen','orderid'));
		$subject = Char_cv($subject);
		$price = Intval($price);
		$fprice = Intval($fprice);
		$ifopen = Intval($ifopen);
		$orderid = Intval($orderid);
		$db->update("INSERT INTO pw_sns_gift_list (subject,price,fprice,ifopen,addtime,orderid) VALUES ('$subject','$price','$fprice','$ifopen','$timestamp',$orderid)");
		$pid = $db->insert_id();

		if ($iconimg = uploadImg($pid)) {
			$db->update("UPDATE pw_sns_gift_list SET icon='$iconimg' WHERE pid='$pid'");
		}
		
		$basename = $basename_p.'&action_p=list';
		adminmsg('operate_success');	
	}
}
elseif($action_p=='edit')
{
	if(empty($_POST['save']))
	{
		InitGP(array('pid'));
		$rt = $db->get_one("Select * From pw_sns_gift_list Where pid =".$pid);
		$src = $rt['icon'] ? " src=$attachpath/hack_gift_goods/{$rt[icon]}" : '';
		ifcheck($rt['ifopen'],'ifopen');
		include(PW_SNS_GIFT.'template/admin.htm');
	}
	else
	{
		InitGP(array('subject','price','fprice','ifopen','pid','orderid'));
		$subject = Char_cv($subject);
		$price = Intval($price);
		$fprice = Intval($fprice);
		$ifopen = Intval($ifopen);
		$orderid = Intval($orderid);
		
		$rt = $db->get_one("Select icon From pw_sns_gift_list Where pid =".$pid);
		if (($iconnew = uploadImg($pid)) && $iconnew <> $rt['icon']) {
			if ($rt['icon']) {
				P_unlink($attachdir.'/hack_gift_goods/'.$rt['icon']);
			}
			$sql = ",icon='$iconnew'";
		}
		$db->update("UPDATE pw_sns_gift_list Set subject='$subject', price=$price, fprice=$fprice, ifopen=$ifopen, orderid=$orderid{$sql} Where pid= ".$pid);
		$basename = $basename_p.'&action_p=list';
		adminmsg('operate_success');	
	}
}
elseif($action_p == 'search'){
	InitGP(array('ser_username','ser_type','ser_go'));
	if($ser_go)
	{
		require_once(R_P.'require/forum.php');
		$db_perpage = 10;
		$page = GetGP('page');
		$page = Intval($page);
		$page < 1 && $page = 1;
		$limit = "LIMIT ".($page-1)*$db_perpage.",".$db_perpage;
		
		$select = array($ser_type=>"selected");
		if($ser_username != '')
		{
			$ser_arr = array();
			$ser_uid = $db->get_value("SELECT uid FROM pw_members Where username='".$ser_username."'");
			if($ser_uid)
			{
				if($ser_type == 'all'){
					$sql = "SELECT * FROM pw_sns_gift_record WHERE fromuid = $ser_uid or touid = $ser_uid $limit";
					$page_sql = "SELECT count(*) as num FROM pw_sns_gift_record WHERE fromuid = $ser_uid or touid = $ser_uid";
				}elseif($ser_type == 'gift'){
					$sql = "SELECT * FROM pw_sns_gift_record WHERE fromuid = $ser_uid $limit";
					$page_sql = "SELECT count(*) as num FROM pw_sns_gift_record WHERE fromuid = $ser_uid";
				}elseif($ser_type == 'receive'){
					$sql = "SELECT * FROM pw_sns_gift_record WHERE touid = $ser_uid $limit";
					$page_sql = "SELECT count(*) as num FROM pw_sns_gift_record WHERE touid = $ser_uid";
				}
				$rt = $db->get_one($page_sql);
				$pages = numofpage($rt['num'],$page,ceil($rt['num']/$db_perpage),"$basename_p&action_p=search&ser_go=1&ser_username=$ser_username&ser_type=$ser_type&");
				
				$query = $db->query($sql);
				while($rt = $db->fetch_array($query))
				{
					$rt['fromuid'] = $db->get_value("SELECT username FROM pw_members Where uid =".$rt['fromuid']);
					$rt['touid'] = $db->get_value("SELECT username FROM pw_members Where uid =".$rt['touid']);
					$rt['img'] = "$attachpath/hack_gift_goods/{$rt[pid]}.gif";
					$ser_arr[] = $rt;
				}
			}
		}
	}
	include(PW_SNS_GIFT.'template/admin.htm');
}


function uploadImg($id) {
	global $attachdir;
	require_once(R_P.'require/postfunc.php');

	$icon = $_FILES['icon'];

	if (!if_uploaded_file($icon['tmp_name'])) {
		return false;
	}
	$savedir	= 'hack_gift_goods';
	createFolder($attachdir.'/'.$savedir);
	
	$attach_ext = "gif";
	$fileuplodeurl = "{$id}.{$attach_ext}";
	$source = $attachdir.'/'.$savedir.'/'.$fileuplodeurl;

	if (!postupload($icon['tmp_name'],$source)) {
		adminmsg('no_uploadfile');
	}
	if (!$img_size = getimagesize($source)) {
		P_unlink($source);
		adminmsg('no_uploadfile');
	}
	return $fileuplodeurl;
}

function createFolder($path) {
	if (!is_dir($path)) {
		createFolder(dirname($path));
		@mkdir($path);
		@chmod($path,0777);
		@fclose(@fopen($path.'/index.html','w'));
		@chmod($path.'/index.html',0777);
	}
}
?>